package com.ruoyi.knowledge.article.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.domain.knowledge.Article;
import com.ruoyi.knowledge.article.mapper.ArticleMapper;
import com.ruoyi.knowledge.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章管理Service业务层处理
 * @author Jm
 * @date 2025-08-12
 */
@Service
public class ArticleServiceImpl implements IArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RedisCache redisCache;


    // 阅读量业务
    @Override
    public void increaseReadCount(Long articleId) {
        articleMapper.updateReadCount(articleId);
    }

    // ================== 实现接口：Redis键生成方法（改为public） ==================
    @Override
    public String likeUserKey(Long articleId) {
        return "article:like:user:" + articleId;
    }

    @Override
    public String likeCountKey(Long articleId) {
        return "article:like:" + articleId;
    }

    @Override
    public String collectUserKey(Long articleId) {
        return "article:collect:user:" + articleId;
    }

    @Override
    public String collectCountKey(Long articleId) {
        return "article:collect:" + articleId;
    }


    // ================== 实现接口：点赞/收藏业务方法（改为public） ==================
    @Override
    public void likeArticle(Long articleId, Long userId) {
        // 防止重复点赞
        Boolean isMember = redisCache.redisTemplate.opsForSet().isMember(likeUserKey(articleId), userId);
        if (Boolean.TRUE.equals(isMember)) {
            throw new RuntimeException("你已经点赞过了");
        }
        redisCache.redisTemplate.opsForSet().add(likeUserKey(articleId), userId);
        redisCache.redisTemplate.opsForValue().increment(likeCountKey(articleId));
    }

    @Override
    public void collectArticle(Long articleId, Long userId) {
        Boolean isMember = redisCache.redisTemplate.opsForSet().isMember(collectUserKey(articleId), userId);
        if (Boolean.TRUE.equals(isMember)) {
            throw new RuntimeException("你已经收藏过了");
        }
        redisCache.redisTemplate.opsForSet().add(collectUserKey(articleId), userId);
        redisCache.redisTemplate.opsForValue().increment(collectCountKey(articleId));
    }

    /**
     * 查询文章管理
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public Article selectArticleById(Long id) {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章管理列表
     * @param article 文章管理
     * @return 文章管理
     */
    @Override
    public List<Article> selectArticleList(Article article) {

        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章管理
     * @param article 文章管理
     * @return 结果
     */
    @Override
    public int insertArticle(Article article) {
        if (article.getDoctor() != null) {
            Long doctorId = article.getDoctor().getId();
            // 存表的时候只存 doctor_id
            article.setDoctorId(doctorId); // ✅ 这行必须加
        }
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章管理
     * @param article 文章管理
     * @return 结果
     */
    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章管理
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Long[] ids) {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章管理信息
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id) {
        return articleMapper.deleteArticleById(id);
    }

}